﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using Zwms.Client.Helper;
using System.IO;
using NPOI.HSSF.UserModel;

namespace Zwms.Client.ManageWms
{
    public partial class TransferBillWnd : UserControl
    {
        private BilTransferBill _queryRecord;
        private BilTransferBill _currentRecord;
        private IList<BilTransferBillDetail> _detailList;
        private IList<BilTransferBillDetail> _subDetailList;
        private IList<BilTransferBillDetail> _deleteDetailList;
        private bool _isNew = false;
        private PagerIndex _pagerIndex;

        public TransferBillWnd()
        {
            InitializeComponent();
            InitBtnImage();
            this.SetTabEditState(false);
            ResetDataPager();
            this._queryRecord = new BilTransferBill();
            this._currentRecord = new BilTransferBill();
            this._detailList = new List<BilTransferBillDetail>();
            this._subDetailList = new List<BilTransferBillDetail>();
            this._deleteDetailList = new List<BilTransferBillDetail>();
            this._pagerIndex = new PagerIndex(1, CommonDefine.iPageSize);
            
        }

        private void InitBtnImage()
        {
            this.btnNew.Image = global::Zwms.Client.Properties.Resources.add;
            this.btnEdit.Image = global::Zwms.Client.Properties.Resources.edit;
            this.btnQuery.Image = global::Zwms.Client.Properties.Resources.view;
            this.btnAddDtl.Image = global::Zwms.Client.Properties.Resources.addall;
            this.btnEditDtl.Image = global::Zwms.Client.Properties.Resources.edit2;
            this.btnDeleteDtl.Image = global::Zwms.Client.Properties.Resources.remove;
            this.btnSave.Image = global::Zwms.Client.Properties.Resources.save;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel;
            this.btnExport.Image = global::Zwms.Client.Properties.Resources.list;
            this.btnSubmit.Image = global::Zwms.Client.Properties.Resources.ok;
            this.btnAudit.Image = global::Zwms.Client.Properties.Resources.check;
        }
        private void ResetDataPager()
        {
            this.dataPager.PageCurrent = 1;
            this.dataPager.PageSize = CommonDefine.iPageSize;
        }

        
        private void SetMenuEditState(bool b)
        {
            btnNew.Enabled = !b;
            btnEdit.Enabled = !b;
            btnQuery.Enabled = !b;
            btnExport.Enabled = !b;

            btnAddDtl.Enabled = b;
            btnEditDtl.Enabled = b;
            btnDeleteDtl.Enabled = b;
            btnSave.Enabled = b;
            btnCancel.Enabled = b;
        }

        private void SetTabEditState(bool b)
        {
            cbTsfType.Enabled = b;
            tbsWarehouse.SetEditState(b);
            dpuPlanDate.SetEditState(b);
            if (b)
            {
                cbTsfType.BackColor = Color.White;
                tbsWarehouse.SetTextBGColor(Color.White);
                dpuPlanDate.SetTextBGColor(Color.White);
            }
            else
            {
                cbTsfType.BackColor = SystemColors.Control;
                tbsWarehouse.SetTextBGColor(SystemColors.Control);
                dpuPlanDate.SetTextBGColor(SystemColors.Control);
            }
        }

        private void SetRecordToUI(BilTransferBill record)
        {
            this.tbTsfBillCode.Text = record.Id;
            if (CommonDefine.DictTsfType.ContainsKey(record.TsfType))
            {
                this.cbTsfType.SelectedIndex = cbTsfType.Items.IndexOf(CommonDefine.DictTsfType[record.TsfType]);
            }
            else
            {
                this.cbTsfType.SelectedIndex = -1;
            }
            this.tbsWarehouse.SetTextBoxContent(record.WarehouseName);
            this.tbsWarehouse.Tag = record.WarehouseCode;
            this.dpuPlanDate.SetDate(CommonHelper.GetDatetimeFromString(record.PlanTsfDate));
            this.dpuTsfDate.SetDate(CommonHelper.GetDatetimeFromString(record.TsfDate));
            string strItem = string.Empty;
            CommonDefine.DictTsfBillStatus.TryGetValue(record.BillStatus,out strItem);
            this.cbBillStatus.SelectedItem = strItem;
            this.tbTsfManName.Text = record.TsfManName;
            this.tbCreateUserName.Text = record.CreateUserName;
            this.dpuCreateDate.SetDate(CommonHelper.GetDatetimeFromString(record.CreateDate));
            this.tbEditUserName.Text = record.EditUserName;
            this.dpuEditDate.SetDate(CommonHelper.GetDatetimeFromString(record.EditDate));
        }

        private BilTransferBill GetRecordFromUI()
        {
            if(string.IsNullOrWhiteSpace(this.tbTsfBillCode.Text.Trim()))
            {
                return null;
            }
            BilTransferBill record = new BilTransferBill();
            record.Id = this.tbTsfBillCode.Text.Trim();
            if (this._isNew)
            {
                if (this.cbTsfType.SelectedIndex >= 0)
                {
                    record.TsfType = CommonHelper.GetDictKeyByValue(CommonDefine.DictTsfType, this.cbTsfType.SelectedItem.ToString().Trim());

                }
                if (!string.IsNullOrWhiteSpace(this.tbsWarehouse.GetTextBoxContent()))
                {
                    record.WarehouseCode = this.tbsWarehouse.Tag.ToString();
                    record.WarehouseName = this.tbsWarehouse.GetTextBoxContent();
                } 
                record.PlanTsfDate = this.dpuPlanDate.SelectDateTime;
               ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
                DateTime dtNow = client1.GetCurrentSystemTime();
                record.CreateUserCode = MainForm.CurrentUser.Id;
                record.CreateUserName = MainForm.CurrentUser.UserName;
                record.CreateDate = Helper.CommonHelper.GetDateString(dtNow);
                record.CreateTime = Helper.CommonHelper.GetTimeString(dtNow);
                record.BillStatus = "0";
            }
            else
            {
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                record = client.QueryTsfBill(record);
                if (this.cbTsfType.SelectedIndex >= 0)
                {
                    record.TsfType = CommonHelper.GetDictKeyByValue(CommonDefine.DictTsfType, this.cbTsfType.SelectedItem.ToString().Trim());
                }
                record.PlanTsfDate = this.dpuPlanDate.SelectDateTime;
               ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
                DateTime dtNow = client1.GetCurrentSystemTime();
                record.EditUserCode = MainForm.CurrentUser.Id;
                record.EditUserName = MainForm.CurrentUser.UserName;
                record.EditDate = Helper.CommonHelper.GetDateString(dtNow);
                record.EditTime = Helper.CommonHelper.GetTimeString(dtNow);
            }
            return record;
        }

        private void ClearUI()
        {
            this.tbTsfBillCode.Text = string.Empty;
            this.cbTsfType.SelectedIndex = -1;
            this.tbsWarehouse.SetTextBoxContent(string.Empty);
            this.dpuPlanDate.SetVoidDate();
            this.dpuCreateDate.SetVoidDate();
            this.dpuEditDate.SetVoidDate();
            this.dpuTsfDate.SetVoidDate();
            this.cbBillStatus.SelectedIndex = -1;
            this.tbTsfManName.Text = string.Empty;
            this.tbCreateUserName.Text = string.Empty;
            this.tbEditUserName.Text = string.Empty;
            this.bilTransferBillDetailBindingSource.DataSource = null;
            this._currentRecord = new BilTransferBill();
        }
        private void LoadSubList(int startIndex, int endIndex)
        {
            this._subDetailList.Clear();
            if (startIndex >= this._detailList.Count)
                return;
            if (endIndex >= this._detailList.Count)
                endIndex = this._detailList.Count - 1;
            BilTransferBillDetail item = new BilTransferBillDetail();
            for (int i = startIndex; i <= endIndex; i++)
            {
                item = this._detailList.ElementAt(i);
                this._subDetailList.Add(item);
            }

        }

        private void LoadDetailToDgv(BilTransferBill record)
        {
            if(record == null || string.IsNullOrWhiteSpace(record.Id))
            {
                return;
            }
            try
            {
                this.Cursor = Cursors.WaitCursor;
                BilTransferBillDetail dtlRecord = new BilTransferBillDetail();
                dtlRecord.TsfBillCode = record.Id;
                _pagerIndex.PageSize = int.MaxValue;
                _pagerIndex.CurrentPageIndex = 1;
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                this._detailList = client.QueryTsfBillDtlList(dtlRecord, ref _pagerIndex);
                this._detailList = this._detailList.ToList();
                this.LoadSubList(0, CommonDefine.iPageSize - 1);
                this.bilTransferBillDetailBindingSource.DataSource = null;
                this.bilTransferBillDetailBindingSource.DataSource = this._subDetailList;
                dataPager.RecordCount = _pagerIndex.RecordCount;
                this.Cursor = Cursors.Default;

            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
            

        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            this.SetMenuEditState(true);
            this.SetTabEditState(true);
            this._isNew = true;
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            this.tbTsfBillCode.Text = client.InitTsfBillCode();
            this.cbBillStatus.SelectedIndex = 0;
            this.tbsWarehouse.Tag = MainForm.CurrentUser.WarehouseCode;
            this.tbsWarehouse.SetTextBoxContent(MainForm.CurrentUser.WarehouseName);
            this.bilTransferBillDetailBindingSource.DataSource = null;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.tbTsfBillCode.Text))
            {
                MessageBox.Show("没有选择任何移库单，请选择移库单进行编辑！");
                return;
            }
            if (cbBillStatus.SelectedIndex != 0)
            {
                MessageBox.Show("单据状态必须为已制单才可编辑！");
                return;
            }
            this._isNew = false;
            this.SetMenuEditState(true);
            this.SetTabEditState(true);
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            ManageWms.TransferBillQueryWnd uc = new TransferBillQueryWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择移库单");
            if (window.ShowDialog() == DialogResult.OK)
            {
                this.SetRecordToUI(uc.SelectedRecord);
                this.LoadDetailToDgv(uc.SelectedRecord);
                this._currentRecord = uc.SelectedRecord;
            }
        }

        private void btnAddDtl_Click(object sender, EventArgs e)
        {
            TransferBillDtlForm window = new TransferBillDtlForm(null, true);
            if (window.ShowDialog() == DialogResult.OK)
            {
                foreach (BilTransferBillDetail item in window.BillDtlList)
                {
                    item.EditFlag = "new";
                    item.TsfBillCode = tbTsfBillCode.Text.Trim();
                    this._detailList.Add(item);
                }
                this.LoadSubList(0, CommonDefine.iPageSize - 1);
                this.bilTransferBillDetailBindingSource.DataSource = null;
                this.bilTransferBillDetailBindingSource.DataSource = this._subDetailList;
                dataPager.RecordCount = this._detailList.Count();
                _pagerIndex.CurrentPageIndex = 1;
                dataPager.PageCurrent = 1;
            }
        }

        private void btnEditDtl_Click(object sender, EventArgs e)
        {
            if (dgv.CurrentCell == null)
                return;
            int rowIndex = dgv.CurrentCell.RowIndex;
            if (rowIndex < 0)
            {
                MessageBox.Show("请选择需要编辑的记录！");
                return;
            }
            BilTransferBillDetail selectRecord = this._subDetailList[rowIndex];
            TransferBillDtlForm window = new TransferBillDtlForm(selectRecord, false);
            if (window.ShowDialog() == DialogResult.OK)
            {
                selectRecord = window.SelectedRecord;
                selectRecord.EditFlag = "edit";
                this._subDetailList.RemoveAt(rowIndex);
                this._subDetailList.Insert(rowIndex, selectRecord);
                this.bilTransferBillDetailBindingSource.DataSource = null;
                this.bilTransferBillDetailBindingSource.DataSource = this._subDetailList;
            }
        }

        private void btnDeleteDtl_Click(object sender, EventArgs e)
        {
            if (dgv.CurrentCell == null)
                return;
            int rowIndex = dgv.CurrentCell.RowIndex;
            if (rowIndex < 0)
            {
                MessageBox.Show("请选择需要删除的记录！");
                return;
            }

            if (MessageBox.Show("是否确认删除所选择的项？", "确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                BilTransferBillDetail itemRecord = this._subDetailList[rowIndex];
                this._deleteDetailList.Add(itemRecord);
                this._detailList.Remove(itemRecord);
                _pagerIndex.CurrentPageIndex = dataPager.PageCurrent;
                int startIndex = CommonDefine.iPageSize * (dataPager.PageCurrent-1);
                int endIndex = startIndex + CommonDefine.iPageSize - 1;
                this.LoadSubList(startIndex, endIndex);
                this.bilTransferBillDetailBindingSource.DataSource = null;
                this.bilTransferBillDetailBindingSource.DataSource = this._subDetailList;
                this.dataPager.RecordCount = this._detailList.Count;
                return;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            BilTransferBill billRecord = this.GetRecordFromUI();
            bool b = false;
            try
            {
                this.Cursor = Cursors.WaitCursor;
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                client.AddTsfBill(billRecord);
                foreach (BilTransferBillDetail item in this._detailList)
                {
                    if (item.EditFlag == "new")
                    {
                        if (client.AddTsfBillDtl(item) == false)
                        {
                            MessageBox.Show("保存失败!");
                            return;
                        }
                    }

                    else if (item.EditFlag == "edit")
                    {
                        if (client.UpdateTsfBillDtl(item) == false)
                        {
                            MessageBox.Show("保存失败!");
                            return;
                        }
                    }
                }

                foreach (BilTransferBillDetail item in this._deleteDetailList)
                {
                    client.DeleteTsfBillDtl(item);
                }
                this.SetMenuEditState(false);
                this.SetTabEditState(false);
                this.ClearUI();
                this.Cursor = Cursors.Default;
                MessageBox.Show("保存成功！");
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定不对当前修改进行保存？", "确定", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                this.SetMenuEditState(false);
                this.SetTabEditState(false);
                this.ClearUI();
            }
            
        }

        private void tbsWarehouse_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.WarehouseWnd uc = new InfoWms.WarehouseWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择仓库");
            uc.SetQueryState();
            if (window.ShowDialog() == DialogResult.OK)
            {
                this.tbsWarehouse.SetTextBoxContent(uc.SelectedRecord.WarehouseName);
                this.tbsWarehouse.Tag = uc.SelectedRecord.Id;
            }
        }

        private void dataPager_BtnClick(object sender, EventArgs e)
        {
            _pagerIndex.CurrentPageIndex = dataPager.PageCurrent;
            int startIndex = _pagerIndex.CurrentPageIndex * CommonDefine.iPageSize;
            int endIndex = startIndex + CommonDefine.iPageSize - 1;
            this.LoadSubList(startIndex, endIndex);
            this.bilTransferBillDetailBindingSource.DataSource = this._subDetailList;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbTsfBillCode.Text))
            {
                MessageBox.Show("请选择需要确认的移库单！");
                return;
            }
            if (cbBillStatus.SelectedIndex != 1)
            {
                MessageBox.Show("单据状态必须为已审核！");
                return;
            }
            try
            {
                //TsfSubmitForm window = new TsfSubmitForm();
                //if (window.ShowDialog() == DialogResult.OK)
                //{

                    this.Cursor = Cursors.WaitCursor;
                    ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                    BilTransferBill billRecord = new BilTransferBill();
                    billRecord.Id = this.tbTsfBillCode.Text.Trim();
                    billRecord = client.QueryTsfBill(billRecord);
                    billRecord.TsfManCode = MainForm.CurrentUser.Id;
                    billRecord.TsfManName = MainForm.CurrentUser.UserName;
                    billRecord.TsfDate = CommonHelper.GetDateString(client.GetCurrentSystemTime());
                    billRecord.BillStatus = "2";
                    client.UpdateTsfBill(billRecord);

                    foreach (BilTransferBillDetail Item in this._detailList)
                    {
                        StoGoodsStock sourceStoRecord = new StoGoodsStock();
                        StoGoodsStock targetStoRecord = new StoGoodsStock();
                        sourceStoRecord.Id = Item.StockCode;
                        sourceStoRecord = client.QueryGoodsStock(sourceStoRecord);
                        sourceStoRecord.PackageQty = sourceStoRecord.PackageQty - Item.PlanPkgGoodsQty;
                        sourceStoRecord.PieceQty = sourceStoRecord.PieceQty - Item.PlanPieceGoodsQty;
                        sourceStoRecord.GoodsTotalQty = sourceStoRecord.PackageQty * sourceStoRecord.GoodsSpec + sourceStoRecord.PieceQty; 
                        if (sourceStoRecord.PackageQty < 0 || sourceStoRecord.PieceQty < 0)
                        {
                            MessageBox.Show("移库数量有误，超出库位原有数量，请核实！");
                            return;
                        }
                        targetStoRecord.GoodsCode = Item.GoodsCode;
                        targetStoRecord.AreaCode = Item.TargetAreaCode;
                        targetStoRecord.ShelfCode = Item.TargetShelfCode;
                        targetStoRecord.LocationCode = Item.TargetLocationCode;
                        PagerIndex pIndex = new PagerIndex(1, 10);
                        IList<StoGoodsStock> targetList = client.QueryGoodsStockList(targetStoRecord,ref pIndex);
                        if (targetList == null || targetList.Count == 0 || string.IsNullOrWhiteSpace(targetList[0].Id))
                        {
                            targetStoRecord = sourceStoRecord;
                            targetStoRecord.Id = client.InitGoodsStockCode();
                            targetStoRecord.PackageQty = Item.PlanPkgGoodsQty;
                            targetStoRecord.PieceQty = Item.PlanPieceGoodsQty;
                            targetStoRecord.GoodsTotalQty = targetStoRecord.PackageQty * targetStoRecord.GoodsSpec + targetStoRecord.PieceQty;
                            targetStoRecord.AreaCode = Item.TargetAreaCode;
                            targetStoRecord.AreaName = Item.TargetAreaName;
                            targetStoRecord.ShelfCode = Item.TargetShelfCode;
                            targetStoRecord.ShelfName = Item.TargetShelfName;
                            targetStoRecord.LocationCode = Item.TargetLocationCode;
                            targetStoRecord.LocationName = Item.TargetLocationName;
                        }
                        else
                        {
                            targetStoRecord.PackageQty = targetStoRecord.PackageQty + Item.PlanPkgGoodsQty;
                            targetStoRecord.PieceQty = targetStoRecord.PieceQty + Item.PlanPieceGoodsQty;
                            targetStoRecord.GoodsTotalQty = targetStoRecord.PackageQty * targetStoRecord.GoodsSpec + targetStoRecord.PieceQty;
                        }
                        client.UpdateGoodsStock(sourceStoRecord);
                        client.UpdateGoodsStock(targetStoRecord);
                        Item.PkgGoodsQty = Item.PlanPkgGoodsQty;
                        Item.PieceGoodsQty = Item.PlanPieceGoodsQty;
                        bool b = client.UpdateTsfBillDtl(Item);

                    }
                    this.Cursor = Cursors.Default;
                
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this.tbTsfBillCode.Text))
            {
                MessageBox.Show("请选择移库单！");
                return;
            }
            SaveFileDialog fileDlg = new SaveFileDialog();
            fileDlg.Filter = "*.xls|*.xls";
            if (fileDlg.ShowDialog() == DialogResult.OK)
            {
                string sTemplate = System.Windows.Forms.Application.StartupPath + @"\Template\移库单模板.xls";
                if (!File.Exists(sTemplate))
                {
                    MessageBox.Show("模板文件不存在！");
                    return;
                }
                string sOutputDir = Path.GetDirectoryName(fileDlg.FileName);
                //打开文件
                FileStream fileTemplate = new FileStream(sTemplate, FileMode.Open, FileAccess.Read);
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(fileTemplate);
                //获取工作簿
                HSSFSheet sheet = hssfworkbook.GetSheet("Sheet1") as HSSFSheet;

                //填写盘点单基本信息
                sheet.GetRow(2).GetCell(2).SetCellValue(this._currentRecord.Id);
                sheet.GetRow(2).GetCell(8).SetCellValue(this.cbBillStatus.SelectedItem.ToString());
                sheet.GetRow(3).GetCell(2).SetCellValue(this._currentRecord.PlanTsfDate);
                sheet.GetRow(3).GetCell(8).SetCellValue(this._currentRecord.TsfDate);
                sheet.GetRow(4).GetCell(2).SetCellValue(this._currentRecord.WarehouseName);
                sheet.GetRow(4).GetCell(8).SetCellValue(this._currentRecord.TsfManName);
                sheet.GetRow(5).GetCell(2).SetCellValue(this._currentRecord.CreateUserName);
                sheet.GetRow(5).GetCell(8).SetCellValue(this._currentRecord.CreateDate);
                //填写明细
                int iRow = 8;
                foreach (BilTransferBillDetail record in this._detailList)
                {
                    sheet.CreateRow(iRow);
                    sheet.GetRow(iRow).CreateCell(0);
                    sheet.GetRow(iRow).CreateCell(1);
                    sheet.GetRow(iRow).CreateCell(2);
                    sheet.GetRow(iRow).CreateCell(3);
                    sheet.GetRow(iRow).CreateCell(4);
                    sheet.GetRow(iRow).CreateCell(5);
                    sheet.GetRow(iRow).CreateCell(6);
                    sheet.GetRow(iRow).CreateCell(7);
                    sheet.GetRow(iRow).CreateCell(8);
                    sheet.GetRow(iRow).CreateCell(9);
                    sheet.GetRow(iRow).CreateCell(10);
                    sheet.GetRow(iRow).CreateCell(11);
                    sheet.GetRow(iRow).GetCell(0).SetCellValue(record.GoodsCode);
                    sheet.GetRow(iRow).GetCell(1).SetCellValue(record.GoodsName);
                    sheet.GetRow(iRow).GetCell(2).SetCellValue(record.SourceAreaName);
                    sheet.GetRow(iRow).GetCell(3).SetCellValue(record.SourceShelfName);
                    sheet.GetRow(iRow).GetCell(4).SetCellValue(record.SourceLocationName);
                    sheet.GetRow(iRow).GetCell(5).SetCellValue(record.TargetAreaName);
                    sheet.GetRow(iRow).GetCell(6).SetCellValue(record.TargetShelfName);
                    sheet.GetRow(iRow).GetCell(7).SetCellValue(record.TargetLocationName);
                    sheet.GetRow(iRow).GetCell(8).SetCellValue(record.PlanPkgGoodsQty);
                    sheet.GetRow(iRow).GetCell(10).SetCellValue(record.PlanPieceGoodsQty);
                    iRow++;
                }
                //保存文件
                try
                {
                    FileStream fileOutput = new FileStream(fileDlg.FileName, FileMode.Create);
                    hssfworkbook.Write(fileOutput);
                    fileOutput.Close();

                    System.Diagnostics.Process.Start(sOutputDir);
                }
                catch (IOException ex)
                {
                    MessageBox.Show("请先关闭已打开的Excel文件。");
                    return;
                }
                finally
                {
                    Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory.ToString();
                }
            }
        }

        private void btnAudit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(tbTsfBillCode.Text))
            {
                MessageBox.Show("请选择需要确认的移库单！");
                return;
            }
            if (cbBillStatus.SelectedIndex != 0)
            {
                MessageBox.Show("单据状态必须为制单！");
                return;
            }
            try
            {
                if (MessageBox.Show("该移库单内容准确无误，审核通过？", "确定", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    this.Cursor = Cursors.WaitCursor;
                    ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                    BilTransferBill billRecord = new BilTransferBill();
                    billRecord.Id = this.tbTsfBillCode.Text.Trim();
                    billRecord = client.QueryTsfBill(billRecord);
                    billRecord.BillStatus = "1";
                    billRecord.AuditorCode = MainForm.CurrentUser.Id;
                    billRecord.AuditorName = MainForm.CurrentUser.UserName;
                    ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
                    DateTime dtNow = client1.GetCurrentSystemTime();
                    billRecord.AuditDate = Helper.CommonHelper.GetDateString(dtNow);
                    client.UpdateTsfBill(billRecord);
                    this.cbBillStatus.SelectedIndex = 1;
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
    }
}
